package com.nulldev.util.uri.client;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;

import com.nulldev.util.java.DataSet;
import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.string.StringsUtil;
import com.nulldev.util.uri.URIUtil;
import com.nulldev.util.uri.iwebkit2.secure.iWebKitSecureModes;
import com.nulldev.util.uri.server.RequestType;
import com.nulldev.util.uri.server.v2.HttpPage2;
import com.nulldev.util.uri.server.v2.HttpServer2;

public class Demo {

	public static void main(String[] args) throws Exception {
		final HttpServer2 https2 = new HttpServer2(8084, false);
		https2.getLogger().traceEnabled = true;
		https2.getLogger().debugEnabled = true;
		final HttpPage2 h2 = new HttpPage2("/test") {
			
			@Override
			public DataSet<Integer, InputStream> request(RequestType type, String url, HashMap<String, String> headers,
					byte[] extraData, String ip) {
				return new DataSet<Integer, InputStream>(200, new ByteArrayInputStream("<html>Hello world!</html>".getBytes()));
			}
		};
		h2.setMimeType("text/html");
		https2.addPage(h2);
		final HttpClient cl = new HttpClient(URIUtil.safeCreate("http://127.0.0.1:8084/test"),
				new HttpClient.HttpClientStringOutput() {
			
			@Override
			public void onData(String data, final int resp) {
				JVMUtil.async_println("Data download complete.");
				JVMUtil.async_println("Data: " + data);
			}
		});
		cl.setThrowOnErrorCode(false);
		cl.setWebKitSecureEnabled(true);
		cl.setiWebKitSecureMode(iWebKitSecureModes.AES_256);
		try {
			cl.run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
